package com.dragon.speech.controller;

import com.dragon.framework.web.BaseController;
import com.dragon.framework.web.ResponseObject;
import com.dragon.speech.common.Constants;
import com.dragon.speech.service.SpeechService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 语音识别
 * Created by jingcai.li on 2017/6/29.
 */
@RestController
public class SpeechController extends BaseController {
    public static final Logger logger = LoggerFactory.getLogger(SpeechController.class);

    @Resource
    private SpeechService speechService;

    /**
     * 通过录音语音获取文本SST,并且检查是否正确
     *
     * @param request
     * @param file
     * @return
     */
    @RequestMapping(value = "/google/speech/to/text", method = RequestMethod.POST)
    public ResponseObject speechToText(HttpServletRequest request, @RequestParam(value = "file", required = false) MultipartFile file) {
        Map<String, Object> reqMap = getRequestMap(request);
        String text = reqMap.get("text").toString();
        try {
            return getSuccessResult(speechService.speechToText(file.getBytes(), text));
        } catch (Exception e) {
            logger.error("调用google出错:" + e.getMessage());
            return getFailureResult(Constants.HTTP_RESPONSE_CODE_GOOGLE_SPEECH_ERROR);
        }
    }

    /**
     * 通过text获取语音MP3
     *
     * @param request
     * @param text
     * @return
     */
    @RequestMapping(value = "/text/to/speech/", method = RequestMethod.GET)
    public ResponseObject textToSpeech(HttpServletRequest request, String text) {
        return null;
    }
}
